package com.fushenlan.epidemicControl.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fushenlan.commons.GeneralResponse;
import com.fushenlan.dto.*;
import com.fushenlan.epidemicControl.config.JwtUtil;
import com.fushenlan.epidemicControl.mapper.NucleinDetectionAbnormalPersonnelMapper;
import com.fushenlan.epidemicControl.mapper.YangManageMapper;
import com.fushenlan.epidemicControl.service.YangManageService;
import com.fushenlan.excel.ExportExcelUtil;
import com.fushenlan.po.NucleinDetectionAbnormalPersonnel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@Service
public class YangManageServiceImpl implements YangManageService {

    @Autowired
    private YangManageMapper yangManageMapper;

    @Autowired
    private NucleinDetectionAbnormalPersonnelMapper nucleinDetectionAbnormalPersonnelMapper;

    /**
     * 导出居民最新情况excel
     */
    private static final String EXPORT_YANG_PERSONAL = "居民最新情况";  //导出excel文件名
    /**
     * 登录人居民表id
     */
    private static final String IN_HABITANT_ID = "inhabitantId";  //登录人居民表id

    /**
     * 居民最新情况查询
     * @param community
     * @param name
     * @param status
     * @param pageSize
     * @param pagerNum
     * @return
     */
    @Override
    public GeneralResponse<IPage<PersonalInfoRespDTO>> queryYangPersonal(Long community, String name, Integer status, Integer pageSize, Integer pagerNum) {
        //获取当前登录人
        Long userId=Long.valueOf(JwtUtil.getUserInfo(IN_HABITANT_ID)).longValue();
        log.info("居民最新情况查询 Request userId->{}",userId,community,name,status);
        IPage<PersonalInfoRespDTO> personalInfoRespDTOPage =yangManageMapper.queryYangPersonal(new Page<>(pagerNum,pageSize),userId,community,name,status);
        log.info("居民最新情况查询 Response personalInfoRespDTOPage->{}",personalInfoRespDTOPage);
        return GeneralResponse.success(personalInfoRespDTOPage);
    }

    /**
     * 居民最新情况查询
     * @return
     */
    @Override
    public GeneralResponse<AllInformationInfoDTO> queryYangNum() {
        //获取当前登录人居民表id
        Long userId=Long.valueOf(JwtUtil.getUserInfo(IN_HABITANT_ID)).longValue();
        log.info("居民最新情况查询 Request userId->{}",userId);
        AllInformationInfoDTO allInformationInfoDTO=yangManageMapper.queryNum(userId);
        log.info("居民最新情况查询 Response allInformationInfoDTO->{}",allInformationInfoDTO);
        return GeneralResponse.success(allInformationInfoDTO);
    }

    /**
     * 转运治愈登记
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public GeneralResponse personalStatusCommit(PersonalStatusCommitReqDTO personalStatusCommitReqDTO) {
        log.info("转运治愈登记 Request personalStatusCommitReqDTO->{}",personalStatusCommitReqDTO);

        NucleinDetectionAbnormalPersonnel nucleinDetectionAbnormalPersonnel=nucleinDetectionAbnormalPersonnelMapper.selectOne(new LambdaQueryWrapper<NucleinDetectionAbnormalPersonnel>()
                .eq(NucleinDetectionAbnormalPersonnel::getUserId,personalStatusCommitReqDTO.getId())
                .eq(NucleinDetectionAbnormalPersonnel::getHealthStatus,1));
        if(nucleinDetectionAbnormalPersonnel==null){
            return GeneralResponse.success("当前人员健康状态有误");
        }
        if (personalStatusCommitReqDTO.getStatus()==2){
            //转运
            nucleinDetectionAbnormalPersonnel.setUserStatus(personalStatusCommitReqDTO.getStatus());
            nucleinDetectionAbnormalPersonnel.setTransportTime(personalStatusCommitReqDTO.getTransportTime());
            nucleinDetectionAbnormalPersonnel.setTransportPlace(personalStatusCommitReqDTO.getTransportPlace());
        }
        if (personalStatusCommitReqDTO.getStatus()==3){
            //治愈
            nucleinDetectionAbnormalPersonnel.setUserStatus(personalStatusCommitReqDTO.getStatus());
            nucleinDetectionAbnormalPersonnel.setHealthStatus(2);
            nucleinDetectionAbnormalPersonnel.setHomeTime(personalStatusCommitReqDTO.getHomeTime());
            nucleinDetectionAbnormalPersonnel.setRemarks(personalStatusCommitReqDTO.getRemarks());
        }
        nucleinDetectionAbnormalPersonnelMapper.updateById(nucleinDetectionAbnormalPersonnel);
        return GeneralResponse.success();
    }

    /**
     * 居民最新情况导出
     * @param community
     * @param name
     * @param status
     * @return
     */
    @Override
    public GeneralResponse exportYangPersonal(Long community, String name, Integer status,
                                              HttpServletResponse response) {
        //获取当前登录人居民表id
        Long userId=Long.valueOf(JwtUtil.getUserInfo(IN_HABITANT_ID)).longValue();
        log.info("未转运信息汇总人员查找 Request userId->{}",userId);
        IPage<PersonalInfoRespDTO> personalInfoRespDTOPage =yangManageMapper.queryYangPersonal(new Page<>(1,-1),userId,community,name,status);
        List<PersonalInfoRespDTO> personalInfoRespDTOList= personalInfoRespDTOPage.getRecords();
        log.info("未转运信息汇总人员查找 Response personalInfoRespDTOList->{}",personalInfoRespDTOList);

        ExportExcelUtil.exportExcel(personalInfoRespDTOList, PersonalInfoRespDTO.class,EXPORT_YANG_PERSONAL,null,response);

        return null;
    }

    @Override
    public GeneralResponse<YangPersonalDetailsDTO> queryYangPersonalDetails(Long userId) {
        //查出当前居民的信息
        YangPersonalDetailsDTO yangPersonalDetailsDTO=yangManageMapper.queryUserInfo(userId);
        //查出所有密接的信息
        List<CloseContacts> closeContactsList=yangManageMapper.queryCloseContactsList(userId);
        yangPersonalDetailsDTO.setCloseContactsList(closeContactsList);
        //查出当前用户所有的检测记录
        List<PersonalDetectionDTO> personalDetectionList=yangManageMapper.queryPersonalDetection(userId);
        yangPersonalDetailsDTO.setPersonalDetectionList(personalDetectionList);
        //查出所有的转运记录
        List<PersonalStatusCommitReqDTO> personalStatusCommitReqDTOList=yangManageMapper.queryPersonalStatus(userId);
        yangPersonalDetailsDTO.setPersonalStatusCommitReqDTOList(personalStatusCommitReqDTOList);
        return GeneralResponse.success(yangPersonalDetailsDTO);
    }


}
